<?php

require 'Mysql.php';

class Membership {
	
	function validate_user($un, $pwd) {
		$mysql = New Mysql();
		$ensure_credentials = $mysql->verify_Username_and_Pass($un, md5($pwd));
		
		if($ensure_credentials) {
			$_SESSION['status'] = $un;
			header("location: index.php");
		} else return "Please enter a correct username and password";
		
	} 
	
	function log_User_Out() {
		if(isset($_SESSION['status'])) {
			unset($_SESSION['status']);
			
			if(isset($_COOKIE[session_name()])) 
				setcookie(session_name(), '', time() - 1000);
				session_destroy();
		}
	}
	
	function confirm_Member() {
		session_start();
		$mysql = New Mysql();
		$check_user = $mysql->check_id($_SESSION['status']);
		if(!$check_user) header("location: login.php");
	}
	
	function confirm_Admin() {
		session_start();
		$mysql = New Mysql();
		$check_user = $mysql->check_Admin($_SESSION['status']);
		if(!$check_user) header("location: index.php");
	}
	
	function get_Profile() {
		$mysql = New Mysql();
		echo $mysql->get_Profile($_SESSION['status']);
	}
	
	function modify_Profile($profile) {
		$mysql = New Mysql();
		$mysql->modify_Profile($_SESSION['status'], $profile);
	}
	
	function modify_Type($username, $type) {
		$mysql = New Mysql();
		$mysql->modify_Type($username, $type);
	}
	
	function get_Tutors(){
		echo "Tutors: <br>";
		$mysql = New Mysql();
		$mysql->get_Tutors();
		echo "<br>";
	}
	
	function get_Students() {
		echo "Students: <br>";
		$mysql = New Mysql();
		$mysql->get_Students();
		echo "<br>";
	}
	
	function find($username) {
		$mysql = New Mysql();
		echo $mysql->get_Profile($username);
	}
	
	function register($un, $pwd, $pwd2) {
		if (!($pwd == $pwd2)) {
			return "Passwords do not match.";
		} else {
			$mysql = New Mysql();
			$success = $mysql->register($un, md5($pwd));
			
			if($success) {
				$_SESSION['status'] = 'authorized';
				header("location: index.php");
			} else return "Username is taken";
		}
	}
}